package com.hqq.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 基本类型消息的队列、交换机、路由、绑定等对象配置类
 * @author huangqiqin
 * @date 2021/04/13 01:08
 **/

@Configuration
public class BasicMessageConfig {

    /**
     * 注入配置文件的环境变量实例
     */
    @Autowired
    private Environment env;

    /**
     * 创建队列
     * @return
     */
    @Bean(name = "basicQueue")
    public Queue basicQueue(){
        return new Queue(env.getProperty("mq.basic.queue.name"), true);
    }

    /**
     * 创建交换机：可持久化数据的交换机
     * @return
     */
    @Bean
    public DirectExchange basicExchange(){
        return new DirectExchange(env.getProperty("mq.basic.exchange.name"), true, false);
    }

    /**
     * 创建绑定：将上面创建的队列绑定到交换机上，并设置路由key
     * @return
     */
    @Bean
    public Binding basicBinding(){
        return BindingBuilder
                .bind(basicQueue())
                .to(basicExchange())
                .with(env.getProperty("mq.basic.routing.key.name"));
    }

}
